﻿IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[esf_files].[P_ListFilesPendingOfOCRIndexing]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [esf_files].[P_ListFilesPendingOfOCRIndexing]
GO

-- esf_files.P_ListFilesPendingOfOCRIndexing '3008'
CREATE PROCEDURE esf_files.P_ListFilesPendingOfOCRIndexing
	@pMonitoredDirectoryIDs VARCHAR (MAX)
AS

	SELECT 
		*
	FROM
		(SELECT 
			FR.ID
			,FR.URL
			,ISNULL (MAX (FROCR.PageIndex), 0) calExtraInfo
			,FR.ItemsCount
		FROM
			esf_files.FileReference FR
		LEFT JOIN
			esf_files.FileReferenceWordHit FROCR ON FROCR.FileReferenceID = FR.ID
		WHERE
			FR.MonitoredDirectoryID IN (SELECT tempItem FROM dbo.FUBSplitString (@pMonitoredDirectoryIDs, ','))
		GROUP BY
			FR.ID
			,FR.URL
			,FR.ItemsCount) X
	WHERE 
		X.calExtraInfo <> X.ItemsCount
	ORDER BY
		X.ItemsCount
		
		
		